package com.ys.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
import org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver;

import javax.annotation.Resource;
import javax.xml.ws.soap.AddressingFeature;
import java.nio.charset.Charset;
import java.util.List;

@Controller
@ComponentScan(basePackages = "com.ys.controller")
@EnableWebMvc
//@EnableAspectJAutoProxy(proxyTargetClass = true)//解决方法限制的权限
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
    @Bean
    public MultipartResolver multipartResolver(){
        MultipartResolver multipartResolver = new StandardServletMultipartResolver();
        return multipartResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver inter = new InternalResourceViewResolver();
        inter.setPrefix("/WEB-INF/jsp/");
        inter.setSuffix(".jsp");
        inter.setOrder(2);
        return inter;
    }

    @Resource
    private DruidDataSource druidDataSource;

    @Bean
    public ViewResolver viewResolver01(){
        JasperReportsViewResolver viewResolver=new JasperReportsViewResolver();
        viewResolver.setPrefix("classpath:/jasper/");
        viewResolver.setSuffix(".jasper");
        viewResolver.setOrder(1);
        viewResolver.setViewNames("a*");
        viewResolver.setViewClass(JasperReportsMultiFormatView.class);
        viewResolver.setJdbcDataSource(druidDataSource);
        return viewResolver;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        返回Spring字符结果集
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        FastJsonConfig config=new FastJsonConfig();
        //        时间类型的处理
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        FastJsonHttpMessageConverter converter= new FastJsonHttpMessageConverter();
        //        上传文件的字符集处理
        converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/json;charset=UTF-8"));
        converter.setFastJsonConfig(config);
        converters.add(converter);
    }
}
